//
//  ZRVisitorView.m
//  SinaByOC
//
//  Created by 张锐 on 16/2/28.
//  Copyright © 2016年 zhangr. All rights reserved.
//

#import "ZRVisitorView.h"
#import <Masonry.h>

@interface ZRVisitorView()

@property (nonatomic,strong) UIImageView * wheelImageView;

@property (nonatomic,strong) UIImageView * centerImageView;

@property (nonatomic,strong) UILabel * messageLabel;

@property (nonatomic,strong) UIButton * registerButton;

@property (nonatomic,strong) UIButton * loginButton;

@property (nonatomic,strong) UIImageView * coverImageView;

@end


@implementation ZRVisitorView

/**
 *  外部接口设置内部变量
 *
 *  @param imageName   图片名称
 *  @param message     文字信息
 *  @param isAnimation 是否有动画
 */
-(void)settintWithImageName:(NSString *)imageName andMessage:(NSString *)message andIfAnimation:(BOOL) isAnimation{

    self.centerImageView.image = [UIImage imageNamed:imageName];
    
    self.messageLabel.text = message;
    
    if (isAnimation) {
        
        [self startAnimation];
    }else{
    
    
        self.wheelImageView.hidden = YES;
    
    }

}

/**
 *  重写构造方法
 */
-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = [[UIColor alloc]initWithWhite:237.0/255.0 alpha:1];
        
        [self setupUI];
        
    }

    return self;

}
/**
 *  添加控件和设置约束
 */
-(void)setupUI{

    //添加控件
    [self addSubview:self.wheelImageView];
    
    [self addSubview:self.coverImageView];
    
    [self addSubview:self.centerImageView];
    
    [self addSubview:self.messageLabel];
    
    [self addSubview:self.registerButton];
    
    [self addSubview:self.loginButton];
    
    //添加约束
    [self.wheelImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    [self.centerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
        
    }];
    
    [self.coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.mas_centerX);
        
        make.top.equalTo(self.wheelImageView.mas_bottom).offset(10);
        
        make.width.mas_equalTo(216);
    }];
    
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.messageLabel.mas_bottom).offset(10);
        
        make.left.equalTo(self.messageLabel.mas_left);
        
        make.width.mas_equalTo(100);
        
        make.height.mas_equalTo(30);
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.messageLabel.mas_bottom).offset(10);
       
        make.right.equalTo(self.messageLabel.mas_right);
        
        make.width.mas_equalTo(100);
        
        make.height.mas_equalTo(30);
    }];

}
/**
 *  动画
 */
-(void)startAnimation{

    CABasicAnimation * baseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    
    baseAnimation.toValue = [NSNumber numberWithDouble: 2 * M_PI];
    
    baseAnimation.repeatCount = MAXFLOAT;
    
    baseAnimation.duration = 10;

    baseAnimation.removedOnCompletion = false;
    
    [self.wheelImageView.layer addAnimation:baseAnimation forKey:nil];

}
/**
 *  懒加载控件
 */

-(UIImageView *)wheelImageView{

    if (_wheelImageView == nil) {
        
        _wheelImageView = [[UIImageView alloc]init];
        _wheelImageView.image = [UIImage imageNamed:@"visitordiscover_feed_image_smallicon"];
        [_wheelImageView sizeToFit];
    }
    return _wheelImageView;
}
-(UIImageView *)centerImageView{

    if (_centerImageView == nil) {
        _centerImageView = [[UIImageView alloc]init];
        
        _centerImageView.image = [UIImage imageNamed:@"visitordiscover_feed_image_house"];
        [_centerImageView sizeToFit];
    }

    return _centerImageView;

}
-(UILabel *)messageLabel{

    if (_messageLabel == nil) {
        _messageLabel = [[UILabel alloc]init];
        _messageLabel.text = @"关注一些人，回这里看看有什么惊喜";
        _messageLabel.textAlignment = NSTextAlignmentCenter;
        _messageLabel.textColor = [UIColor darkGrayColor];
        _messageLabel.numberOfLines = 0;
        _messageLabel.font = [UIFont systemFontOfSize:14];
        
    }

    return _messageLabel;

}
-(UIButton *)registerButton{

    if (_registerButton == nil) {
        _registerButton = [[UIButton alloc]init];
        [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
        [_registerButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [_registerButton setBackgroundImage: [UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        [_registerButton addTarget:self action:@selector(registerButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return  _registerButton;
}
-(UIButton *)loginButton{

    if (_loginButton == nil) {
        _loginButton = [[UIButton alloc]init];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        [_loginButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [_loginButton setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        [_loginButton addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }

    return _loginButton;
}
-(UIImageView *)coverImageView{

    if (_coverImageView == nil) {
        _coverImageView = [[UIImageView alloc]init];
        
        _coverImageView.image = [UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"];
        [_coverImageView sizeToFit];
    }

    return _coverImageView;

}


/**
 *  注册和登录按钮点击事件
 */

-(void)registerButtonClick{
 
    //调用注册代码块
    if(self.registerBlock){
    
        self.registerBlock();
    
    }
    
    
}
-(void)loginButtonClick{
    
    //发送通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"loginButtonClick" object:self];


}


@end
